--[[
带图标的漂浮文字
]]
local idle_text = {}
local idle_max = 100
for _ = 1, idle_max do
    local panel = class.panel.create('',0,0,200,32)
    panel.icon = panel:add_panel('',-32,0,32,32)
    panel.text = class.text:builder {
        parent = panel,
        text = '',
        color = 0xffffffff,
        shadow_offset_x = 4,
        shadow_offset_y = -4,
        shadow_color = 0xff000000,
        show_flag = true,
		font_size = 20,
        align = 4,
    }
    table.insert(idle_text, panel)
end


local texttag = {}
setmetatable(texttag, texttag)
texttag.group = {}

-- 结构
local mt = {}
texttag.__index = mt

-- 类型
mt.type = 'ui tag'

-- 句柄
mt.handle = nil

-- 文本内容
mt.text = '无文本'

-- 缩放
mt.scale = 1

-- 初始位置
mt.point = ac.point(0, 0)

-- 速度
mt.speed = 0

-- 角度
mt.angle = 90

-- 颜色
mt.color = 0xffffffff
mt.alpha = 1

mt.clock = 0

-- 生命周期
mt.life = 3

-- 淡化
mt.fade = 2

-- 可见性
mt.show = true



-- 设置图标
function mt:set_icon(icon)
	if icon then
		self.icon = icon
    end
    if self.handle then
        self.handle.icon:set_normal_image(self.icon)
    end
end

-- 设置文本
function mt:set_text(string)
	if string then
		self.text = string
    end

    if self.handle then
        self.handle.text:set_text(self.text)
    end
end

-- 设置大小
function mt:set_scale(scale)
	if scale then
		self.scale = scale
    end
	self.handle:set_relative_size(self.scale)
end


-- 设置位置
mt.offset_x = 0
mt.offset_y = 0
function mt:set_position(position)
	if not self.handle then
		return
	end
	if position then
		self.point = position
	end
    local point = self.point:get_point()
	local x, y, z = point:get()
	
    local sx, sy = ac.frame.w2s(x, y, z + point:get_height())

    if sx == nil or sx < 0 or sy < 32 or sx > 1920 or sy > 768 then
        self.handle:hide()
    else
        self.handle:set_real_position(sx + self.offset_x,sy - self.offset_y)
        self.handle:show()
    end
end

-- 设置颜色
function mt:set_color(color)
    if self.handle then
        self.handle.text:set_color(color or self.color)
    end
end

-- 透明度
function mt:set_alpha(alpha)
    if self.handle then
	    self.handle:set_alpha(alpha or self.alpha)
    end
end

-- 移除漂浮文字
function mt:remove()
	if self.removed then
		return
    end
    
	self.removed = true
    
	self.handle:hide()
	self.handle:unbind_world()
	
	table.insert(idle_text, self.handle)
	
	texttag.group[self] = nil
	
end

--[更新]
mt.jump_rate = 0.5
mt.jump_height = 25
mt.angle = 90
mt.speed = 0

function mt:update(delta)
	self.clock = self.clock + delta
	if self.clock > self.life - self.fade then
		if self.clock > self.life  then
			self:remove()
			return
		else
			self.alpha = math.max(self.alpha - self.fade * delta,0)
			self:set_alpha()
		end
	end
    if self.life>0 and self.jump_time then
        local time = self.clock
        if time<=self.jump_time then
            local rate = math.sin(time/self.jump_time * 180)
            local size = rate * self.jump_rate
            self.init_size = self.init_size or self.scale
            self:set_scale(self.init_size * (1 + size))
        end
    end
    if self.speed~=0 then
        self.offset_x = self.offset_x + delta*self.speed * math.cos(self.angle)
        self.offset_y = self.offset_y + delta*self.speed * math.sin(self.angle)
    end
	self:set_position()
end



ac.icon_tag = function(data)
    setmetatable(data, texttag)
    local handle = table.remove(idle_text,1)
    if not handle then
        texttag.removed = true
        return texttag
	end
    data.handle = handle
    data:set_text()
    data:set_icon()
    data:set_position()
	data:set_scale()

    texttag.group[data] = true
    return data
end




ac.hardware:on("界面绘制")(function(_,delta)
    for tt in pairs(texttag.group) do
        tt:update(delta)
    end
end)

return texttag

